<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!--    <div th:replace="~{commons/script::_css}"></div>-->
    <dvi th:replace="~{commons/script::script}"></dvi>
</head>
<body>


<div id="tools" style="margin-bottom: 20px">
    <form action="/leads/searchLeads">
        <table>
            <tr>
                <td>
                    <i class="iconfont"></i>
                    <select name="batchOperate" style="width:100px;height:25px;" id="batchOperate">
                        <option value="-1">--批量操作--</option>
                        <option value="0">
                            批量删除
                        </option>
                    </select>
                </td>
                <td>
                    <select name="filterSearch" style="width:100px;height:25px;" id="searchBy">
                        <option value="-1">--请选择筛选条件--</option>
                        <option value="0">公司名</option>
                        <option value="1">联系人姓名</option>
                    </select>
                    <select name="contain" style="width:60px;height:25px;">
                        <option value="-1">--包含--</option>
                    </select>
                </td>
                <td class="text-content">
                    <input name="search" type="text" placeholder="请输入查询关键字" id="searchText">
                    <input type="submit" id="search" class="easyui-linkbutton"
                           data-options="iconCls:'icon-search',plain:true">搜索</input>
                </td>
                <td>
                    <span id="chat" class="easyui-linkbutton" data-options="iconCls:'icon-cut',plain:true">发送短信</span>
                    <span id="email" class="easyui-linkbutton" data-options="iconCls:'icon-redo',plain:true">发送邮件</span>
                </td>

                <td>
                    <!-- manage/customer/CustomerAction_showAdd.action-->
                    <span onclick="return add('/leads/toAddLeads')" class="easyui-linkbutton"
                          data-options="iconCls:'icon-add',plain:true">新建客户</span>
                    <span id="tool" class="easyui-linkbutton" data-options="iconCls:'icon-cog',plain:true">客户工具</span>
                </td>
            </tr>
        </table>
    </form>

</div>
<div id="customerLs" width="100%" class="easyui-datagrid"></div>
</body>

<script type="text/javascript">
    $("#search").on("click", function (event) {
        event.preventDefault();
        var searchBy = $("#searchBy").val();
        var searchText = $("#searchText").val();
        $("#customerLs").datagrid("load", {
            "searchBy": $("#searchBy").val(),
            "searchText": $("#searchText").val()
        })
    })

    $(function () {
        //分页展示客户列表
        $("#customerLs").datagrid({
            url: "/leads/listLeadsChi",
            checkOnSelect: false,
            idField: "customerId",
            pagination: true,
            sortName: "customerId",
            columns: [
                [
                    {field: "选择", checkbox: true,width:80},
                    {field: "id", hidden: true,width:80},
                    // {field:"follow",title:"关注",
                    //     formatter:function(value,row,index){
                    //         var isfollowed = false;
                    //
                    //         $.ajax({
                    //             type: "post",
                    //             url: "manage/customer/CustomerAction_isfollowed.action",
                    //             data:{"customerId":row.id},
                    //             dataType: "json",
                    //             async : false,
                    //             success:function(data){
                    //                 if(data&&data.success){
                    //                     isfollowed=true;
                    //                     return;
                    //                 }
                    //             }
                    //         });
                    //
                    //
                    //         if(!isfollowed){
                    //             return "<img onclick='javascript:follow(" +'"' + row.id +'"' +","+index+")' src='static/easyui/themes/icons/stark.png'/>";
                    //         }else{
                    //             return "<img onclick='javascript:unfollow(" +'"' + row.id +'"' +","+index+")' src='static/easyui/themes/icons/star.png'/>";
                    //         }
                    //     }},
                    {field: "name", title: "公司名",width:80},
                    {field: "contactsName", title: "联系人姓名",width:80},
                    {field: "saltname", title: "尊称",width:80},
                    {field: "mobile", title: "手机",width:80},
                    {field: "nextstep", title: "下次联系内容",width:80},
                    {field: "nextstepTime", title: "下次联系时间",width:80},
                    // {field:"ownerName",title:"负责人",width:80},
                    {field: "createName", title: "创建人",width:80},
                    {field: "createTime", title: "创建时间",width:80},
                    // {field:"diffDay",title:"据到期天数",width:80},

                    // {field:"address",title:"客户联系地址",formatter:function(value){
                    //         return "<span title='"+value+"'>"+value+"</span>"
                    //     }},
                    // {field:"owner",title:"负责人"},
                    // {field:"createUser",title:"创建人"},
                    // {field:"createTime",title:"创建时间",width:50,formatter:function(value){
                    //         return "<span title='"+value+"'>"+value+"</span>"
                    //     }},
                    // {field:"updateTime",title:"更新时间",width:100,formatter:function(value){
                    //         if(value){
                    //             return "<span title='"+value+"'>"+value+"</span>";
                    //         }
                    //         return "";
                    //     }},
                    // {field:"days",title:"距离到期天数"},
                    {
                        field: "operation", title: "操作", width: 150, formatter: function (value, rowData, index) {
                            return "<a href='javascript:void(0);' onclick=\"return see(\'/leads/showLeads\',\'" + rowData.id + "\')\">查看</a>" +
                                "<a href='javascript:void(0);' onclick=\"return toAddCustomer(\'/customer/cus\',\'" + rowData.id + "\')\">转换</a>" +
                                "<a href='javascript:void(0);' onclick=\"return draw(\'/leads/drawLeads\',\'" + rowData.id + "\')\">领取</a>" +
                                "<a href='javascript:void(0);' onclick=\"return fenPei(\'/leads/panDuan\',\'" + rowData.id + "\')\">分配</a>" +
                                " <a href='javascript:void(0);' onclick=\"return modify(\'/leads/toUpdateLeads\',\'" + rowData.id + "\')\">编辑</a>"
                        }
                    }

                ]
            ],
            loadFilter: function (data) {
                return {total: data.data.totalRows, rows: data.data.result}
            }

        })

    })

    /**
     * 分配线索
     * @param url
     * @param id
     */
    function fenPei(url,id){
        $.post(url,
            {
                "id": id
            },
            function (data) {
                if (data && data.success) {
                    parent.parent.openTopWindow({
                        "url":"/leads/toUpdateLeads?id=" + id,
                        "width": 800,
                        "height": 700,
                        "title": "客户信息",
                        close: function () {
                            $("#customerLs").datagrid("reload");
                        }
                    })
                    return;
                }
                $.messager.alert("警告", data.message);
            }, "json");
    }
    //领取线索
    function draw(url,id){
        $.messager.confirm("警告", "请确认领取", function (b) {
            if (b) {
                $.post(url,
                    {
                        "id": id
                    },
                    function (data) {
                        if (data && data.success) {
                            $.messager.alert("提示", data.message);
                            $("#customerLs").datagrid("reload");
                            return;
                        }
                        $.messager.alert("警告", data.message);
                    }, "json");
            }
        });
    }
    //删除按钮事件处理函数
    function del(url) {
        //获取到选中的一行数据
        var rows = $("#customerLs").datagrid("getChecked");
        if (!rows.length) {
            $.messager.alert("警告", "请选择要删除的数据");
            return;
        }
        $.messager.confirm("警告", "数据删除后无法恢复，是否确认删除", function (b) {
            if (b) {
                var ids = new Array();
                $.each(rows, function (index, obj) {
                    ids.push(obj.id);
                });
                //将数组中元素使用,分割拼接为一个字符串
                ids = ids.join(",");
                $.post(url,
                    {
                        "ids": ids
                    },
                    function (data) {
                        if (data && data.success) {
                            $.messager.alert("提示", data.message);
                            $("#customerLs").datagrid("reload");
                            return;
                        }
                        $.messager.alert("警告", data.message);
                    }, "json");
            }
        });
    }

    //查询全部客户
    function whole(url) {
        $("#customerLs").datagrid("load", {})
    }



    //线索转换
    function transformed() {
        $("#customerLs").datagrid("load", {
            "transformed": true
        })
    }

    //页面添加客户
    function add(url) {
        parent.parent.openTopWindow({
            "url": url,
            "width": 800,
            "height": 700,
            "title": "新建客户",
            close: function () {
                $("#customerLs").datagrid("reload");
            }
        })
        // location.href=url;
    }

    //修改客户
    function modify(url, id) {
        parent.parent.openTopWindow({
            "url": url + "?id=" + id,
            "width": 800,
            "height": 700,
            "title": "新建客户",
            close: function () {
                $("#customerLs").datagrid("reload");
            }
        })
    }

    //转换成客户
    function toAddCustomer(url, id) {
        parent.parent.openTopWindow({
            "url": url + "?leadsId=" + id,
            "width": 800,
            "height": 700,
            "title": "客户信息",
            close: function () {
                $("#customerLs").datagrid("reload");
            }
        })
    }

    //查看客户
    function see(url, id) {
        parent.parent.openTopWindow({
            "url": url + "?id=" + id,
            "width": 800,
            "height": 700,
            "title": "客户信息",
            close: function () {
                $("#customerLs").datagrid("reload");
            }
        })
        // window.location.href=url+"?id="+id
    }
</script>
<script type="text/javascript">
    $(function () {
        //邮件发送
        $("#email").on("click", function (e) {
            var rows = $("#customerLs").datagrid("getSelections");
            if (!rows || rows.length == 0) {
                $.messager.alert("警告", "请选中要发送邮件的客户");
                return;
            }
            if (rows.length > 1) {
                $.messager.alert("警告", "请选择单条数据进行发送邮件");
                return false;
            }
            var userId = rows[0].id;

            location.href = "/leads/toSendEmail?id=" + userId;
        })
        $("#batchOperate").bind("change", function () {
            var value = $(this).val();
            if (value == 0) {
                del('/leads/deleteLeads')
            }

        })
    })

</script>
</html>
